package com.sztz.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcelFactory;
import com.sztz.entity.CargoItem;
import com.sztz.entity.HkTdec;
import com.sztz.entity.HkTdecCargo;
import com.sztz.entity.Rocars;
import com.sztz.excel.HkTdecExcelListener;
import com.sztz.excel.MyExcelListener;
import com.sztz.vo.CargoItemExcelVo;
import com.sztz.vo.HkTdecExcelVo;
import com.sztz.vo.RocarsExcelVo;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.OptionalInt;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * Excel解析工具类
 * @author yzy
 * @date 2022/7/14 18:16
 */
@Slf4j
public class ExcelReadUtils {
    /**
     * 导入香港货物模板二数据
     * @author yzy
     * @date 2022/7/14 18:16
     */
    public static Rocars readRocarTemplate2Excel(File file){
        RocarsExcelVo rocarVo = new RocarsExcelVo();
        List<CargoItemExcelVo> list = new ArrayList<>();
        Consumer<List<Map<Integer,Object>>> rocarsConsumer = data->{
            OptionalInt first = IntStream.range(0, data.size()).filter(i -> data.get(i).values().contains("Goods Item No. *")).findFirst();
            int cargoItemSeqNo = first.isPresent() ? first.getAsInt() : 0;
            if(cargoItemSeqNo > 0) {
                RocarsUtils.convertToRocars(RocarsUtils.mapToRocarPropertyMap(data, cargoItemSeqNo), rocarVo);
                List<Map<Integer, Object>> cargoItemList = CollUtil.sub(data, cargoItemSeqNo, data.size());
                RocarsUtils.convertToCargoItemList(cargoItemList,list);
            }
        };
        int batchSize= 60;
        int sheetNo=0;
        int maxRowNum=200;
        MyExcelListener myExcelListener = new MyExcelListener.Builder(rocarsConsumer).setBatchCount(batchSize).setMaxReadRowNum(maxRowNum).exceptionRead(Boolean.FALSE).build();
        EasyExcelFactory.read(file, myExcelListener).sheet(sheetNo).doRead();
        Rocars rocars = new Rocars();
        BeanUtil.copyProperties(rocarVo,rocars);
        List<CargoItem> collect = list.stream().map(item -> {
            CargoItem cargoItem = new CargoItem();
            BeanUtil.copyProperties(item, cargoItem);
            return cargoItem;
        }).collect(Collectors.toList());
        rocars.setCargosItems(collect);
        return rocars;
    }
    /**
     * 导入香港清关模板数据
     * @author yzy
     * @date 2022/08/23 10:33
     */
    public static List<HkTdec> readHkTdecTpl(File file){
        List<HkTdec> list = new ArrayList<>();
        HkTdec hkTdec = new HkTdec();
        Consumer<List<HkTdecExcelVo>> hkTdecConsumer = data -> {
            if(StrUtil.isBlank(hkTdec.getType())) {
                HkTdecExcelVo hkTdecExcelVo = data.get(0);
                BeanUtil.copyProperties(hkTdecExcelVo, hkTdec);
            }
            List<HkTdecCargo> collect = data.stream().map(item -> {
                HkTdecCargo hkTdecCargo = new HkTdecCargo();
                BeanUtil.copyProperties(item, hkTdecCargo);
                return hkTdecCargo;
            }).collect(Collectors.toList());
            hkTdec.setHkTdecCargoList(collect);
            list.add(hkTdec);
        };
        HkTdecExcelListener hkTdecExcelListener = new HkTdecExcelListener(hkTdecConsumer);
        hkTdecExcelListener.setBatchCount(99);
        EasyExcelFactory.read(file, HkTdecExcelVo.class, hkTdecExcelListener).headRowNumber(2).autoTrim(Boolean.TRUE).sheet().doRead();
        return list;
    }
}
